//
//  TuCaoViewController.m
//  ChunLaiZhuangClient
//
//  Created by zouzhisheng on 16/4/24.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "TuCaoViewController.h"
#import "SZAddImage.h"
@interface TuCaoViewController (){
    NSMutableArray *dataArray;
    int  upCout;
    NSMutableArray *upLoadImages;//上传成功后的图片地址
}
@property (weak, nonatomic) IBOutlet UIView *mobileTFBg;
@property (weak, nonatomic) IBOutlet UITextField *mobileTF;

@property (weak, nonatomic) IBOutlet UIView *contentTFBg;
@property (weak, nonatomic) IBOutlet UITextView *contentTF;
@property (weak, nonatomic) IBOutlet UILabel *placeLabel;
@property (weak, nonatomic) IBOutlet SZAddImage *addImageViews;
@end

@implementation TuCaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mobileTFBg.layer.cornerRadius=4;
    self.mobileTFBg.layer.borderWidth=0.5;
    self.mobileTFBg.layer.borderColor=[UIColor colorWithHexString:@"c7c7cc"].CGColor;
    self.contentTFBg.layer.cornerRadius=4;
    self.contentTFBg.layer.borderWidth=0.5;
    self.contentTFBg.layer.borderColor=[UIColor colorWithHexString:@"c7c7cc"].CGColor;
    [self setNaviTitle:@"请君吐槽"];
    [self setNaviLeftButton:@""];
    [self setNaviRightButton:@"提交"];
    upLoadImages=[[NSMutableArray alloc] init];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.view addGestureRecognizer:tap];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)tapAction:(UITapGestureRecognizer*)tap{
    [self.mobileTF resignFirstResponder];
    [self.contentTF resignFirstResponder];
   }
-(void)clickRightButton:(UIButton*)bt{
    if (![self.mobileTF.text isMobileNumber]) {
        [[LKDHud shareHud] showtip:@"请输入正确的手机号"];
        return;
    }
    if (self.contentTF.text.length==0) {
        [[LKDHud shareHud] showtip:@"吐槽内容不能为空"];
        return;
    }
    
    dataArray=[[NSMutableArray alloc] init];
    for (int i=0; i<self.addImageViews.images.count; i++) {
        UIImage *image=self.addImageViews.images[i];
        NSData *data = UIImageJPEGRepresentation(image,1.0);
        NSString *aPath=[NSString stringWithFormat:@"%@/Documents/%d.png",NSHomeDirectory(),i];
        [data writeToFile:aPath atomically:YES];
        [dataArray addObject:aPath];
    }
    if (dataArray.count>0) {
      upCout=0;
      [[LKDHud shareHud] showLoadingView];
      [self upLoadImage:dataArray[upCout]];
    }else{
     [[LKDHud shareHud] showLoadingView];
       [self addTuCaoContent];
    }
}

-(void)upLoadImage:(NSString*)imagePath{
    [ApiClient upLoadTuCaoImages:imagePath didFinishLoaded:^(KXJson *json, NSString *path) {
        if ([[json getStringForKey:@"status"] intValue]==1) {
            KXJson *dataJson=[json getJsonForKey:@"data"];
            [upLoadImages addObject:[dataJson getStringForKey:@"thumb"]];
            upCout++;
            if (upCout>=dataArray.count) {
                [self addTuCaoContent];
                return ;
            }
            [self upLoadImage:dataArray[upCout]];
        }
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        
    }];

}
-(void)addTuCaoContent{
    [ApiClient addTuCaoMobile:self.mobileTF.text content:self.contentTF.text images:upLoadImages didFinishLoaded:^(KXJson *json, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
        if ([[json getStringForKey:@"status"] intValue]==1) {
            [self.navigationController popViewControllerAnimated:YES];
            [[LKDHud shareHud] showtip:[json getStringForKey:@"message"]];
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSString *resultingString = [textView.text stringByReplacingCharactersInRange: range withString: text];
    if (resultingString.length==0) {
        _placeLabel.hidden=NO;
    }else{
        _placeLabel.hidden=YES;
    }
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView{
    
    if ([textView.text length] == 0) {
        _placeLabel.hidden=NO;
    }else{
        _placeLabel.hidden=YES;
    }
}

@end
